/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.gui.widget;

import java.awt.Component;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.tnmk.worktime.gui.utils.WindowUtils;

/**
 * The inside openable object's show/hide will be handled automatically.
 * @author tnmkhoi
 */
public class AutoHideHandler {

  private final OpenableHandler openable;
  private int showPeriod;

  /**
   * @param openable
   * @param showPeriod measure unit is SECOND. After this period, the Pop-up
   * will be hide automatically.
   */
  public AutoHideHandler(OpenableHandler openable, int showPeriod) {
    this.openable = openable;
    this.showPeriod = showPeriod;
    Component comp = openable.getComponent();
    openable.setLocation(WindowUtils.getRightX(comp), WindowUtils.getBottomY(comp));
  }

  public void show() {
    openable.show();

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    scheduler.schedule(new Runnable() {
      @Override
      public void run() {
        openable.hide();
      }
    }, showPeriod, TimeUnit.SECONDS);
  }

  public void hideImediately() {
    openable.hide();
  }

  public int getShowPeriod() {
    return showPeriod;
  }

  public void setShowPeriod(int showPeriod) {
    this.showPeriod = showPeriod;
  }
}
